
package com.sfdlJPush.utils;

/*
 * [文 件 名]:Subscriber.java
 * [创 建 人]:allenwang
 * [创建时间]:Dec 31, 2013
 * [编　　码]:UTF-8
 * [版　　权]:Copyright © 2012 B5Msoft Co,Ltd. 
 */
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

/**
 * [简要描述]:监听同一事件的多处变化 [详细描述]:
 * 
 * @author [allenwang]
 * @email [allen.wang@b5m.com]
 * @version [版本号,Dec 31, 2013]
 * @see [Subscriber]
 * @package [com.sfdlJPush.afw.eventbug]
 * @since [afw]
 */
public class EventBus
{
    private static volatile EventBus defaultInstance;
    private final WeakHashMap<Object, List<Object>> notificateMaps;

    public static EventBus getInstance()
    {
        if (defaultInstance == null)
        {
            synchronized (EventBus.class)
            {
                if (defaultInstance == null)
                {
                    defaultInstance = new EventBus();
                }
            }
        }
        return defaultInstance;
    }

    public EventBus()
    {
        notificateMaps = new WeakHashMap<Object, List<Object>>();
    }

    // 此函数的设计，是注册你要监听的事件名称，监听对象，一般放在onResume里面，activity,fragment均可
    public void subscribe(Object notificationName , SubscriberChangeListener subscriberChangeListener)
    {
        unSubscribe(notificationName, subscriberChangeListener);
        List<Object> subscribedEvents = notificateMaps.get(notificationName);
        if (subscribedEvents == null)
        {
            subscribedEvents = new ArrayList<Object>();
            notificateMaps.put(notificationName, subscribedEvents);
        }
        subscribedEvents.add(subscriberChangeListener);
    }

    // 订阅某一个观察者中的多个事件
    public void subscribes(SubscriberChangeListener subscriberChangeListener , Object... notificationNames)
    {
        for (int i = 0; i < notificationNames.length; i++)
        {
            subscribe(notificationNames[i], subscriberChangeListener);
        }
    }

    // 此函数的设计，是取消注册时监听的事件名称，监听对象，一般放在onDestory里面,千万不要放在onPause里面，不然会监听不到事件。activity,fragment均可
    public void unSubscribe(Object notificationName , SubscriberChangeListener subscriberChangeListener)
    {
        List<Object> subscribedEvents = notificateMaps.get(notificationName);
        if (subscribedEvents != null)
        {
            if (subscribedEvents.contains(subscriberChangeListener))
            {
                subscribedEvents.remove(subscriberChangeListener);
            }
            if (subscribedEvents.isEmpty())
            {
                notificateMaps.remove(subscriberChangeListener);
            }
        }
    }

    // 取消订阅某一个观察者中的多个事件
    public void unSubscribes(SubscriberChangeListener subscriberChangeListener , Object... notificationNames)
    {
        for (int i = 0; i < notificationNames.length; i++)
        {
            unSubscribe(notificationNames[i], subscriberChangeListener);
        }
    }

    // 此函数的设计，通知监听了事件名称的对象集，进行对象的更改。activity,fragment均可
    public void notifiDataUpdate(Object notificationName , Object notificateContent)
    {
        List<Object> subscribedEvents = notificateMaps.get(notificationName);
        if (subscribedEvents != null)
        {
            for (Object eventType : subscribedEvents)
            {
                SubscriberChangeListener subscriberChangeListener = (SubscriberChangeListener) eventType;
                subscriberChangeListener.onSubscriberDataChanged(notificationName, notificateContent);
            }
        }
    }

    // 当有事件发生时的，会回调这个函数
    public static interface SubscriberChangeListener
    {
        public void onSubscriberDataChanged(Object notificationName , Object notificateContent);
    }
}
